﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Model.CSP;
using CSP.Common.Web;
using CSP.Model.CSPEnum;
using CSP.Bll;
using CSP.Bll.IObjectBlls;

namespace CSP.Web.MyHome.Credit
{
    public partial class RatingConsume : CSPMyHome
    {
        private const string SERVICEID_KEY = "SERVICEID_KEY";
        private int ServiceInfoId
        {
            get
            {
                return (int)ViewState[SERVICEID_KEY];
            }
            set
            {
                ViewState[SERVICEID_KEY] = value;
            }
        }

        private const string CONSUMENAME_KEY = "CONSUMENAME_KEY";
        private string ConsumeUserName
        {
            get
            {
                return ViewState[CONSUMENAME_KEY].ToString();
            }
            set
            {
                ViewState[CONSUMENAME_KEY] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindOrderUI();
            }
        }

        protected void Btn_Submit_ServerClick(object sender, EventArgs e)
        {
            CreditLine creditLine = GetCreditLineByUI();
            CreateCreditLine(creditLine);
        }

        private void BindOrderUI()
        {
            string orderNumber = Request["ordernumber"].ToString();
            Order order = GetOrderByOrderNumber(orderNumber);
            if (order == null)
            {
                this.FailRedirect("抱歉当前订单不存在", "/MyHome/ConsumeOrder/List.aspx");
            }
            this.LB_ServiceTitle.Text = order.ServiceTitle;
            this.LB_ConsumeUser.Text = order.OrderUserName;
            this.U_UserCredit.UserName = order.OrderUserName;
            ServiceInfoId = order.ServiceInfoId;
            ConsumeUserName = order.OrderUserName;
        }


        /// <summary>
        /// 通过UI获取评价信誉明细
        /// </summary>
        private CreditLine GetCreditLineByUI()
        {
            CreditLine creditLine = new CreditLine();
            creditLine.ServiceInfoId = ServiceInfoId;
            //好评
            if (this.R_Good.Checked)
            {
                creditLine.CreditRating = (byte)(int)CreditRatingType.Good;
            }
            else if (this.R_General.Checked)
            {
                creditLine.CreditRating = (byte)(int)CreditRatingType.General;
            }
            else if (this.R_Bad.Checked)
            {
                creditLine.CreditRating = (byte)(int)CreditRatingType.Bad;
            }
            creditLine.FromUsername = UserProvider.UserName;
            creditLine.ToUserName = ConsumeUserName;
            creditLine.ServiceAttitude = 0;
            creditLine.ServiceAbility = 0;
            creditLine.RatingContent = this.Txt_RatingContent.Value;
            creditLine.RatingType = 1;
            creditLine.RatingTime = DateTime.Now;
            creditLine.OrderNumber = Request["OrderNumber"].ToString();
            creditLine.ServiceInfoId = ServiceInfoId;
            return creditLine;
        }

        /// <summary>
        /// 根据订单编号 获取订单信息
        /// </summary>
        /// <param name="orderNumber"></param>
        /// <returns></returns>
        private Order GetOrderByOrderNumber(string orderNumber)
        {
            CSPBllFactory bllFactory = CSPBllFactory.GetInstance();
            Order result = null;
            using (IOrderBll orderBll = bllFactory.GetOrderBll())
            {
                result = orderBll.GetByOrderNumberAndConsumeUser(orderNumber, UserProvider.UserName);
            }
            return result;
        }

        /// <summary>
        /// 创建信誉明细
        /// </summary>
        /// <param name="creditLine"></param>
        private void CreateCreditLine(CreditLine creditLine)
        {
            try
            {
                CSPBllFactory bllFactory = CSPBllFactory.GetInstance();
                using (ICreditLineBll creditLineBll = bllFactory.GetCreditLineBll())
                {
                    creditLineBll.Create(creditLine);
                }
            }
            catch (Exception ex)
            {
                this.LoggerProvider.Error(ex);
                this.FailRedirect(ex.Message, "/");
            }
            this.SuccessfullRedirect("已完成评价成功", "/");
        }
    }
}